<?php

class App_Model_DbTable_Account_OAuth extends Ext_Db_Table  
{  
	/** Table name */  
    protected $_name = 'account_oauth';
    
    private static $_accountOAuthRow = array();
    
	public function getRow($oauthId)
	{
		if(!$oauthId)
		{
			return "";
		}
		
		if(isset(self::$_accountOAuthRow[$oauthId]))
		{
			return self::$_accountOAuthRow[$oauthId];
		}
		
		$db = $this->getAdapter();
		$safe = $db->quoteInto(" id = ? ", $oauthId);
		$row = $this->fetchRow($safe);
		if (!$row) {
			return "";
		}
		$_accountOAuthRow = $row->toArray();

		self::$_accountOAuthRow[$oauthId] = $_accountOAuthRow;
		
		return $_accountOAuthRow;
	}
	
	public function addNew($inputArray)
	{
		$sql_data_array = array();
		if(array_key_exists('id', $inputArray)) $sql_data_array['id'] = $inputArray['id'];
		if(array_key_exists('userId', $inputArray)) $sql_data_array['userId'] = $inputArray['userId'];
		if(!$sql_data_array)
		{
			return "";
		}
		
		try {
			$rows_affected = $this->insert($sql_data_array);
		}catch (Exception $e){
			trigger_error($e->getMessage(), E_USER_WARNING);
		}
	}
	
	public function addOld($inputArray, $oauthId)
	{
		$sql_data_array = array();
		if(array_key_exists('userId', $inputArray)) $sql_data_array['userId'] = $inputArray['userId'];
		if(!$sql_data_array)
		{
			return "";
		}
		
		try {
			$db = $this->getAdapter();
			$where = $db->quoteInto(" id = ? ", $oauthId);
			$rows_affected = $this->update($sql_data_array, $where);
			if($rows_affected)
			{
				unset(self::$_accountOAuthRow[$oauthId]);
			}
		}catch (Exception $e){
			trigger_error($e->getMessage(), E_USER_WARNING);
		}
	}
	
}  

